package v1

import (
	"github.com/gin-gonic/gin"
	"go-programming-tour-book/blog-service/global"
	"go-programming-tour-book/blog-service/internal/service"
	"go-programming-tour-book/blog-service/pkg/app"
	"go-programming-tour-book/blog-service/pkg/convert"
	"go-programming-tour-book/blog-service/pkg/errcode"
)

type Tag struct {
}

func NewTag() Tag {
	return Tag{}
}

// @Summary 获取多个标签
// @Produce json
// @Param name query string false "标签名称" maxlength(100)
// @Param state query int false "状态" Enums(0,1) default(1)
// @Param page query int false "页码"
// @Param page_size query int false "每页数量"
// @Success 200 {object} model.TagSwagger "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/tags [GET]
func (t Tag) GetTag(c *gin.Context) {
	param := service.TagListRequest{}

	response := app.NewResponse(c)

	valid, errs := app.BindAndValid(c, &param)

	if !valid {
		global.Logger.Fatalf("app.BindAndValid errs:%v", errs)

		errRsp := errcode.InvalidParams.WithDetails(errs.Errors()...)

		response.ToErrorResponse(errRsp)

		return
	}

	svc := service.New(c.Request.Context())

	pager := app.Pager{Page: app.GetPage(c), PageSize: app.GetPageSize(c)}

	totalRows, err := svc.Countag(&service.CountTagRequest{
		Name:  param.Name,
		State: param.State,
	})

	if err != nil {
		global.Logger.Fatalf("svc.countTag err :%v", err)

		response.ToErrorResponse(errcode.ErrorCountTagFail)

		return
	}

	tags, err := svc.GetTagList(&param, &pager)

	if err != nil {
		global.Logger.Fatalf("svc.GetTagList err : %v", err)

		response.ToErrorResponse(errcode.ErrorGetTagListFail)

		return
	}

	response.ToResponseList(tags, totalRows)

	return
}

// @Summary 新增标签
// @Produce json
// @Param name body string true "标签名称" minlength(3) maxlength(100)
// @Param state body int false "状态" Enums(0,1) default(1)
// @Param created_by body string true "创建人" minlength(3) maxlength(100)
// @Success 200 {object} model.TagSwagger "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/tags [POST]
func (t Tag) AddTag(c *gin.Context) {
	param := service.CreateTagRequest{}

	response := app.NewResponse(c)

	valid, errs := app.BindAndValid(c, &param)

	if !valid {
		global.Logger.Fatalf("bind fail:%v", errs)

		response.ToErrorResponse(errcode.ErrorCreateTagFail)

		return
	}

	svc := service.New(c.Request.Context())

	err := svc.CreateTag(&param)

	if err != nil {
		global.Logger.Fatalf("svc create fail:%v", err)

		response.ToErrorResponse(errcode.ErrorCreateTagFail)

		return
	}

	response.ToResponse(gin.H{})

	return
}

// @Summary 更新标签
// @Produce json
// @Param id path int true "标签ID"
// @Param name body string false "标签名称"
// @Param state body int false "状态" Enums(0,1) default(1)
// @Param modified_by body string false "修改人" minlength(3) maxlength(100)
// @Success 200 {object} model.TagSwagger "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/tags/{id} [PUT]
func (t Tag) UpdateTag(c *gin.Context) {
	param := service.UpdateTagRequest{
		ID: convert.StrTo(c.Query("id")).MustUInt32(),
	}

	response := app.NewResponse(c)

	valid, errs := app.BindAndValid(c, &param)

	if !valid {
		global.Logger.Fatalf("bind valid fail:%v", errs)

		response.ToErrorResponse(errcode.ErrorUpdateTagFail)

		return
	}

	svc := service.New(c.Request.Context())

	err := svc.UpdateTag(&param)

	if err != nil {
		global.Logger.Fatalf("update tag fail:%v", err)

		response.ToErrorResponse(errcode.ErrorUpdateTagFail)

		return
	}

	response.ToResponse(gin.H{})

	return

}

// @Summary 更新标签上下架状态
// @Produce json
// @Param id path int true "标签ID"
// @Param state body int false "状态" Enums(0,1) default(1)
// @Param modified_by body string false "修改人" minlength(3) maxlength(100)
// @Success 200 {object} model.TagSwagger "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/tags/{id}/state [PATCH]
func (t Tag) UpdateTagState(c *gin.Context) {

}

// @Summary 删除标签
// @Product json
// @Param id query int true "标签ID"
// @Success 200 {object} model.TagSwagger "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failture 500 {object} errcode.Error "内部错误"
// @Router /api/v1/tags/{id} [delete]
func (t Tag) DeleteTag(c *gin.Context) {
	param := service.DeleteTagRequired{
		ID: convert.StrTo(c.Query("id")).MustUInt32(),
	}

	response := app.NewResponse(c)

	valid, errs := app.BindAndValid(c, &param)

	if !valid {
		global.Logger.Fatalf("valid,delete fail:%v", errs)

		response.ToErrorResponse(errcode.ErrorDeleteTagFail)

		return
	}

	svc := service.New(c.Request.Context())

	err := svc.DeleteTag(&param)

	if err != nil {
		global.Logger.Fatalf("delete fail:$v", err)

		response.ToErrorResponse(errcode.ErrorDeleteTagFail)

		return
	}

	response.ToResponse(gin.H{})

	return
}
